<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="patientIdentityNo">
        <el-input
          v-model="queryParams.patientIdentityNo"
          placeholder="请输入患者身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="patientList" @selection-change="handleSelectionChange" :default-sort = "{prop: 'patientNo', order: 'descending'}">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50"/>
      <el-table-column label="患者编号" align="center" prop="patientNo" sortable/>
      <el-table-column label="患者身份证号" align="center" prop="patientIdentityNo"/>
      <el-table-column label="患者姓名" align="center" prop="patientName">
        <template slot-scope="scope">
          <router-link :to="'/patient/detail/' + scope.row.patientId" class="link-type">
            <span>{{ scope.row.patientName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="患者年龄" align="center" prop="patientAge"/>
      <el-table-column label="患者性别" align="center" prop="patientGender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.patientGender"/>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="patientPhone"></el-table-column>
      <el-table-column label="患者状态" align="center" prop="patientStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.patient_status" :value="scope.row.patientStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder"
            @click="register(scope.row)"
          >登记
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {listPatient, getPatient, delPatient, addPatient, updatePatient, insertPatient} from "@/api/gaoxin/patient";
import Recorder from "js-audio-recorder";
import {addClinic, listClinic, updateClinic} from "@/api/gaoxin/clinic";
import {getUserAll} from "@/api/system/user";

export default {
  name: "Patient",
  dicts: ['patient_status','sys_user_sex','marital_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 患者表格数据
      patientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 患者查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: null,
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
    this.getDicts("marital_status").then(res=>{
      this.marital_status=res.data;
    })
  },
  methods: {
    /** 查询患者列表 */
    getList() {

      let query = {status: 0};
      listClinic(query).then(response => {
        let clinicList = response.rows;
        console.log("source-----"+clinicList[0].source);
        let length = response.total;
        for (let i = 0; i < length; i++) {
          if (clinicList[i].source) {

          }
        }
      });

      // this.loading = true;
      // this.queryParams.patientStatus = '1'; //患者未就诊
      // listPatient(this.queryParams).then(response => {
      //   this.patientList = response.rows;
      //   this.total = response.total;
      //   for (let i = 0; i < this.total; i++) {
      //     let patientId = this.patientList[i];
      //     let query = {pa}
      //   }
      //   this.loading = false;
      // });
    },
    getInfo() {
      getUserAll().then(response => {
        this.userOptions = response.users;
        console.log('userOptions',this.userOptions)
      });
    },
    register(row) {
      let patientId = row.patientId;
      this.$router.push("/workflow/register/add/" + patientId);
    },
    discharge(row) {
      let patientId = row.patientId;
      let query = {patientId: patientId, status: '0'};
      listClinic(query).then(response => {
        console.log("1-----"+response.rows[0])
      });
      this.$router.push("/workflow/register/add/" + patientId);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patientId: null,
        patientNo: null,
        patientName: null,
        patientBirthday: null,
        patientAge: null,
        patientGender: null,
        patientIdentityNo: null,
        patientJob: null,
        patientWeight: null,
        patientHeight: null,
        patientFolk: null,
        patientAddress: null,
        patientPhone: null,
        patientMarriage: null,
        patientStatus: "0",
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情按钮操作 */
    patdetail(row) {
      console.log('row是啥：',row)
      var temp = {
        name: 'patient_detail',
        params: {
          pat: row
        }
      }
      this.$router.push(temp)
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.patientId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/workflow/register/new");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patientId = row.patientId || this.ids
      getPatient(patientId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改患者";
      });
    },
  }
};
</script>
